<template>
  <div>
    <van-cell-group>
      <van-field v-model="user.phoneNumber" required clearable label="手机号" placeholder="请输入手机号" />

      <van-field v-model="user.password" type="password" label="密码" placeholder="请输入密码" required />
    </van-cell-group>

    <van-row>
      <van-col span="24" style="text-align:center;padding: 20px;">
        <van-button round type="danger" size="large" @click="submitLogin">登录</van-button>
      </van-col>
    </van-row>
    <van-row>
      <van-col span="16"></van-col>
      <van-col span="8">
        <a @click="toRegister()">新用户注册</a>
      </van-col>
    </van-row>
  </div>
</template>

<script>
import { Field, Cell, CellGroup, Button, Row, Col, Toast, Divider } from "vant";
import { apiLogin } from "@/api/user";
import { YM_LOGIN_BACK_URL, ACCESS_TOKEN } from "@/store/constant";
export default {
  components: {
    [Field.name]: Field,
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
    [Button.name]: Button,
    [Row.name]: Row,
    [Col.name]: Col,
    [Toast.name]: Toast,
    [Divider.name]: Divider
  },

  data() {
    return {
      user: {
        phoneNumber: "",
        password: ""
      },
      backData: null
    };
  },
  methods: {
    submitLogin() {
      apiLogin(this.user).then(res => {
        if (res.status === 0) {
          sessionStorage.setItem(ACCESS_TOKEN, res.data);
          const backUrl = sessionStorage.getItem(YM_LOGIN_BACK_URL);
          window.location.href = backUrl;
        } else {
          Toast(res.msg);
        }
      });
    },
    toRegister() {
      this.$router.push({
        name: "register",
        params: {}
      });
    }
  },
  created() {
    this.backData = this.$route.params.backData;
    console.log(this.backData);
  }
};
</script>